<script setup lang="ts">
import { ref } from 'vue'
import { addProductByBatchUsingPost } from '@/api/productController.ts'
import { message } from 'ant-design-vue'
import { useRouter } from 'vue-router'

const router = useRouter()

const form = ref<API.ProductFetchDto>({})
const loading = ref(false)

const handleSubmit = async () => {
  loading.value = true
  const res = await addProductByBatchUsingPost(form.value)
  if (res.code === 200 && res.data) {
    message.success('批量新增成功')
    await router.push('/admin/product-manage')
  } else {
    message.error(res.message)
  }
  loading.value = false
}
</script>

<template>
  <div id="product-batch-add-view">
    <h2 style="margin-bottom: 16px">批量新增商品</h2>
    <a-form layout="vertical" :model="form" @finish="handleSubmit">
      <a-form-item label="关键词" name="searchText">
        <a-input v-model:value="form.searchText" placeholder="请输入关键词" />
      </a-form-item>
      <a-row :gutter="16" justify="space-between">
        <a-col :span="7">
          <a-form-item label="新增数量" name="count">
            <a-input-number
              v-model:value="form.count"
              placeholder="请输入数量"
              :min="1"
              :max="30"
              style="min-width: 180px"
            />
          </a-form-item>
        </a-col>
        <a-col :span="7">
          <a-form-item label="价格下限" name="priceDown">
            <a-input-number
              v-model:value="form.priceDown"
              placeholder="请输入价格下限"
              :min="1"
              :max="10000"
              style="min-width: 180px"
            />
          </a-form-item>
        </a-col>
        <a-col :span="7">
          <a-form-item label="价格上限" name="priceUp">
            <a-input-number
              v-model:value="form.priceUp"
              placeholder="请输入价格上限"
              :min="1"
              :max="10000"
              style="min-width: 180px"
            />
          </a-form-item>
        </a-col>
      </a-row>
      <a-form-item label="名称前缀" name="namePrefix">
        <a-input
          v-model:value="form.namePrefix"
          placeholder="请输入名称前缀，会自动补充序号"
          allow-clear
        />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit" style="width: 100%" :loading="loading">
          执行任务
        </a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<style scoped>
#product-batch-add-view {
  max-width: 720px;
  margin: 0 auto;
}
</style>
